package com.spring.sunflower.encrypt;

//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import lombok.extern.log4j.Log4j2;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.commons.lang3.exception.ExceptionUtils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.spring.sunflower.util.LogUtils;

import java.io.UnsupportedEncodingException;

/**
 * guoxingfa
 * 10:22 2021/11/18
 */
public final class DataEncryptUtils {

    /**
     * @return java.lang.String
     * @Author guoxingfa
     * @Description // 返回数据进行加密
     * @Date 10:22 2021/11/18
     * @Param businessData 返回数据
     **/
    public static String dataEncrypt(Object businessData) {
        if (businessData == null) {
            return null;
        }
        String base64Encoder;
        try {
            String strData = "{\"age\":20,\"answerRate\":100,\"authenticate\":1,\"backgroundWall\":[],\"badEvaluateTotal\":0,\"birthday\":880905600000,\"city\":\"深圳市\",\"constellation\":\"射手座\",\"dueTime\":1584276040000,\"educationV\":[{\"color\":\"\",\"id\":2850,\"name\":\"专科\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"education\"}],\"fans\":1,\"favorite\":2,\"freeCall\":0,\"giftAmount\":0,\"goodEvaluateTotal\":0,\"grade\":7,\"guildId\":10031,\"height\":185,\"hobbiesList\":[{\"color\":\"8B572A\",\"id\":26,\"name\":\"茶道\",\"orderId\":\"9\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"6DCEE7\",\"id\":2792,\"name\":\"上网\",\"orderId\":\"27\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"FA4BB1\",\"id\":2793,\"name\":\"演戏\",\"orderId\":\"28\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"FBA314\",\"id\":22,\"name\":\"收藏\",\"orderId\":\"13\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"6DCEE7\",\"id\":2797,\"name\":\"飞行\",\"orderId\":\"32\",\"remark\":\"\",\"type\":\"hobbies\"}],\"housingV\":[],\"id\":10008,\"incomeV\":[{\"color\":\"\",\"id\":3035,\"name\":\"3-5万\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"income\"}],\"integral\":0,\"isFavorite\":0,\"isVip\":0,\"lastOnLineTimeDesc\":\"6 个月前\",\"maritalStatusV\":[{\"color\":\"\",\"id\":2920,\"name\":\"已婚\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"maritalStatus\"}],\"memberId\":10008,\"mobile\":\"176****4750\",\"money\":953951,\"nativeCity\":\"深圳市\",\"nativeProvince\":\"广东省\",\"nickname\":\"42号童鞋的\",\"occupationV\":[{\"color\":\"\",\"id\":2863,\"name\":\"金融\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"occupation\"}],\"onlineMoodV\":[{\"color\":\"\",\"id\":2847,\"name\":\"等待呼叫\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"onlineMood\"}],\"openTime\":1579783240000,\"photo\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/app/10008/1617894372670.jpeg\",\"photoVOList\":[],\"price\":0,\"promotionBalance\":\"邀请获得：0.0 元\",\"promotionShareVO\":{\"photo\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/yuyue.png\",\"subTitle\":\"喜欢Ta的样子还是声音？随时可以匿名聊天，想聊就聊，想见就见。\",\"title\":\"No1:好玩的视频聊天APP\",\"url\":\"http://www.wanyuyue.com/invite/index3.html?uId=104066\"},\"promotionUrl\":\"http://www.wanyuyue.com/invite/index3.html?uId=?token=d79031b70488f3fdc5963cebed16d7a4&mid=104066\",\"province\":\"广东省\",\"remark\":\"0\",\"sex\":\"1\",\"signature\":\"狙击手计算机三级去啊具体\",\"star\":0,\"status\":0,\"verifyCount\":0,\"videoDetailVOList\":[],\"visualizeLabels\":[],\"voiceFavoriteTotal\":20,\"voiceSignature\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/app/10008/1618655759964.amr\",\"voiceSignatureDuration\":0,\"wealth\":50056,\"weight\":71}";
            byte[] bytes = new Gson().toJson(strData).getBytes("utf-8");

//            byte[] bytes = JSON.toJSONString(businessData, SerializerFeature.DisableCircularReferenceDetect).getBytes(Constants.ENCODE_UTF_8);
            byte[] encrypt = DES.encrypt(bytes, DES.PASSWORD_KEY);
//            base64Encoder = Base64.java8Base64Encoder(encrypt);
            byte[] encode = Base64.encode(encrypt);
            base64Encoder = new String(encode);
        } catch (UnsupportedEncodingException e) {
//            log.error(ExceptionUtils.getStackTrace(e));
            throw new RuntimeException(e);
        }
        LogUtils.INSTANCE.logI("AppRetrofit", base64Encoder);
        return base64Encoder;
    }


    /**
     * @param businessData
     * @return
     * @throws Exception
     */
    public static String dataDecrypt(String businessData) {
        if (TextUtils.isEmpty(businessData)) {
            return null;
        }
//        java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
//        byte[] base64Encoder =decoder.decode(businessData);
        byte[] base64Encoder = Base64.decode2(businessData);
        byte[] decryResult = new byte[0];
        try {
            decryResult = DES.decrypt(base64Encoder, DES.PASSWORD_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            String result = "{\"status\":0,\"message\":\"很遗憾,数据解析失败\"}";
            return result;
        }

        String result = new String(decryResult);
        result = result.replaceAll("\\\\","");
        result = result.replace("\"{", "{");
        result = result.replace("}\"", "}");
        LogUtils.INSTANCE.logI("AppRetrofit", "修复=" + result);
        LogUtils.INSTANCE.logI("AppRetrofit", "解密后=" + result);
        return result;
    }

//    public static void main(String[] args) throws Exception {
//        String strData = "{\"age\":20,\"answerRate\":100,\"authenticate\":1,\"backgroundWall\":[],\"badEvaluateTotal\":0,\"birthday\":880905600000,\"city\":\"深圳市\",\"constellation\":\"射手座\",\"dueTime\":1584276040000,\"educationV\":[{\"color\":\"\",\"id\":2850,\"name\":\"专科\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"education\"}],\"fans\":1,\"favorite\":2,\"freeCall\":0,\"giftAmount\":0,\"goodEvaluateTotal\":0,\"grade\":7,\"guildId\":10031,\"height\":185,\"hobbiesList\":[{\"color\":\"8B572A\",\"id\":26,\"name\":\"茶道\",\"orderId\":\"9\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"6DCEE7\",\"id\":2792,\"name\":\"上网\",\"orderId\":\"27\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"FA4BB1\",\"id\":2793,\"name\":\"演戏\",\"orderId\":\"28\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"FBA314\",\"id\":22,\"name\":\"收藏\",\"orderId\":\"13\",\"remark\":\"\",\"type\":\"hobbies\"},{\"color\":\"6DCEE7\",\"id\":2797,\"name\":\"飞行\",\"orderId\":\"32\",\"remark\":\"\",\"type\":\"hobbies\"}],\"housingV\":[],\"id\":10008,\"incomeV\":[{\"color\":\"\",\"id\":3035,\"name\":\"3-5万\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"income\"}],\"integral\":0,\"isFavorite\":0,\"isVip\":0,\"lastOnLineTimeDesc\":\"6 个月前\",\"maritalStatusV\":[{\"color\":\"\",\"id\":2920,\"name\":\"已婚\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"maritalStatus\"}],\"memberId\":10008,\"mobile\":\"176****4750\",\"money\":953951,\"nativeCity\":\"深圳市\",\"nativeProvince\":\"广东省\",\"nickname\":\"42号童鞋的\",\"occupationV\":[{\"color\":\"\",\"id\":2863,\"name\":\"金融\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"occupation\"}],\"onlineMoodV\":[{\"color\":\"\",\"id\":2847,\"name\":\"等待呼叫\",\"orderId\":\"\",\"remark\":\"\",\"type\":\"onlineMood\"}],\"openTime\":1579783240000,\"photo\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/app/10008/1617894372670.jpeg\",\"photoVOList\":[],\"price\":0,\"promotionBalance\":\"邀请获得：0.0 元\",\"promotionShareVO\":{\"photo\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/yuyue.png\",\"subTitle\":\"喜欢Ta的样子还是声音？随时可以匿名聊天，想聊就聊，想见就见。\",\"title\":\"No1:好玩的视频聊天APP\",\"url\":\"http://www.wanyuyue.com/invite/index3.html?uId=104066\"},\"promotionUrl\":\"http://www.wanyuyue.com/invite/index3.html?uId=?token=d79031b70488f3fdc5963cebed16d7a4&mid=104066\",\"province\":\"广东省\",\"remark\":\"0\",\"sex\":\"1\",\"signature\":\"狙击手计算机三级去啊具体\",\"star\":0,\"status\":0,\"verifyCount\":0,\"videoDetailVOList\":[],\"visualizeLabels\":[],\"voiceFavoriteTotal\":20,\"voiceSignature\":\"https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/app/10008/1618655759964.amr\",\"voiceSignatureDuration\":0,\"wealth\":50056,\"weight\":71}";
//        String dataEncrypt = dataEncrypt(strData);
//        System.out.println("----------------------加密后-----------------------------------");
//        System.out.println(dataEncrypt);
//        System.out.println("-----------------------加密后----------------------------------");
//        //String dataEncrypt = "a7xYht0JMT2oIk+x8vIQZKMoJTHHcAZLkCKjm1vyGbZ7+QdtzteN7g4Wh+b0maF18UtlqgdoffF/ylOv/XrttAc3KXgeIS8JWFdvOUZFRxzDS90fUjUqWwA161o1RKuj/1cR22x5ed8Q0ex+TGCWILjb84ZFviBifrKb9I4e4Vdb3CNtwr0yUAnQ7dqTj3W+timcdgOv2nuKZIv3HyKo6kM9uzpKRfzelRuVfS7j0aObWaf0537z37Xt9nQry7mq9JkRzWJNdir6vrWsv3CeW7B4oqJpRzgZpM/CfAKMhZbUEnBh8MJtE/yVxMthl1MjZzedSNK4SNab1uxHhgKeBvmCQupd4GOvQEWAK5PNgghNOcYvNVa1qYRd0eDBTmF/e1h/LDGnAFFYeX0975cBwtM75936Lc8UCg6HNDRAmGtAElD0+rm1F79UtIqTLuXmjjvAFMw5b4xFFXvMiE1bRqMeb9ps/ImAUfZO4PJAvFVa7ZoP+byAM6UzdmM6/M72L4tBISdPs9cK1CBCs9ajUj5pCCsvngrtpNqZOIEjNMNq1+q6VdF5Gl4hv+hPLEgeZya+4WOiwe1jMdPW1AkODcT6o9fgNV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